//package com.rjwh.dingdong.client.activity.player;
//
//import android.content.ComponentName;
//import android.content.Context;
//import android.content.Intent;
//import android.content.ServiceConnection;
//import android.os.Bundle;
//import android.os.IBinder;
//import android.util.Log;
//import android.view.LayoutInflater;
//import android.view.View;
//import android.view.ViewGroup;
//import android.widget.AdapterView;
//import android.widget.ImageView;
//import android.widget.ListAdapter;
//import android.widget.ListView;
//import android.widget.TextView;
//
//import com.rjwh.dingdong.client.R;
//import com.rjwh.dingdong.client.activity.BaseActivity;
//import com.rjwh.dingdong.client.activity.player.fragment.PlayerToolbarFragment;
//import com.rjwh.dingdong.client.bean.localbean.PlayList;
//import com.rjwh.dingdong.client.bean.localbean.Song;
//import com.rjwh.dingdong.client.player.PlayConstants;
//import com.rjwh.dingdong.client.player.PlayState;
//import com.rjwh.dingdong.client.service.PlaybackService;
//import com.rjwh.dingdong.client.util.RxBus;
//import com.rjwh.dingdong.client.util.ViewHolder;
//import com.rjwh.dingdong.client.widget.RoundImageView;
//import com.wby.base.AdapterBase;
//
//import rx.Observable;
//import rx.Subscriber;
//
///**
// * 项目名称：whrjwh-parent
// * 类描述：
// * 创建人：wby
// * 创建时间：2016/10/26 9:21
// * 修改人：wby
// * 修改时间：2016/10/26 9:21
// * 修改备注：
// *
// * @version V1.0
// */
//public class PlayerMusicActivity extends BaseActivity {
//
//    ListView listView;
//
//    PlaybackService mPlaybackService;
//
//    private int currentIndex = -1;
//    CommunityPostMsgAdapter adapter;
//
//    Observable<Song> observableUpdateSong;
//    Observable<PlayState> observablePlayState;
//
//    @Override
//    protected void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        setContentView(R.layout.player_list);
//        startService(new Intent(this, PlaybackService.class));
//        bindService(new Intent(this, PlaybackService.class), mConnection, Context.BIND_AUTO_CREATE);
//
//
//        getSupportFragmentManager().beginTransaction().replace(R.id.fm, PlayerToolbarFragment.getInstance()).commit();
//
//
//        listView = (ListView) findViewById(R.id.listview);
//        adapter = new CommunityPostMsgAdapter(this);
//        listView.setAdapter(adapter);
//
//        final PlayList playList = new PlayList();
//
//        playList.addSong(new Song("光辉岁月", "http://abv.cn/music/光辉岁月.mp3"));
//        playList.addSong(new Song("红豆", "http://abv.cn/music/红豆.mp3"));
//        playList.addSong(new Song("千千阙歌", "http://abv.cn/music/千千阙歌.mp3"));
//
//
//        adapter.appendToList(playList.getSongs());
//
//
//        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
//                Song stSong = adapter.getList().get(i);
//                Song song = mPlaybackService.getPlayingSong();
//                mPlaybackService.play(playList, i);
//                currentIndex = i;
//                updataListViewSelectState(i);
//            }
//        });
//        init();
////
////        //这里延时联网获取数据加载到了数据就去刷新列表状态
////        //绑定服务成功以后去刷新列表中的状态
////        if(adapter!=null && !adapter.isEmpty()){
////            updataListViewSelectState(mPlaybackService.getPlayingSong());
////        }
//
//    }
//
//    public void init() {
//        observableUpdateSong = RxBus.get().register(PlayConstants.ACTION_UPDATA_SONG, Song.class);
//        observablePlayState = RxBus.get().register(PlayConstants.ACTION_PLAY_STATUS_CHANGED, PlayState.class);
//        addSubscription(observableUpdateSong, subscriberUpdateSong);
//        addSubscription(observablePlayState, subscriberPlayState);
//    }
//
//    @Override
//    protected void onDestroy() {
//        super.onDestroy();
//        unbindService(mConnection);
//        RxBus.get().unregister(PlayConstants.ACTION_UPDATA_SONG, observableUpdateSong);
//        RxBus.get().unregister(PlayConstants.ACTION_PLAY_STATUS_CHANGED, observablePlayState);
//    }
//
//    private ServiceConnection mConnection = new ServiceConnection() {
//        public void onServiceConnected(ComponentName className, IBinder service) {
//            mPlaybackService = ((PlaybackService.LocalBinder) service).getService();
//            //绑定服务成功以后去刷新列表中的状态
//            if(adapter!=null && !adapter.isEmpty()){
//                updataListViewSelectState(mPlaybackService.getPlayingSong());
//            }
//        }
//
//        public void onServiceDisconnected(ComponentName className) {
//            mPlaybackService = null;
//        }
//    };
//
//    public class CommunityPostMsgAdapter extends AdapterBase<Song> implements ListAdapter {
//        private LayoutInflater inflater;
//        private Context mContext;
//        private int selectIndex = -1;
//        PlayState pt = PlayState.READY;
//
//        public CommunityPostMsgAdapter(Context context) {
//            this.mContext = context;
//            inflater = LayoutInflater.from(context);
//        }
//
//        @Override
//        protected View getExView(int arg0, View arg1, ViewGroup arg2) throws Exception {
//            if (arg1 == null) {
//                arg1 = inflater.inflate(R.layout.community_post_msg_item, null);
//            }
//            RoundImageView headIV = ViewHolder.get(arg1, R.id.item_community_post_msg_head_iv);
//            TextView name_TV = ViewHolder.get(arg1, R.id.item_community_post_msg_user_name_tv);
//            TextView contentTV = ViewHolder.get(arg1, R.id.item_community_post_msg_content);
//            ImageView item_community_post_msg_alert_point = ViewHolder.get(arg1, R.id.item_community_post_msg_alert_point);
//            Song topicNotice = mList.get(arg0);
//            name_TV.setText("");
//            if (selectIndex == arg0) {
//                switch (pt) {
//                    case READY:
//                        name_TV.setText("准备中 开始播放");
//                        break;
//                    case PLAY:
//                        name_TV.setText("播放中");
//                        break;
//                    case PAUSE:
//                        name_TV.setText("暂停");
//                        break;
//                    case STOP:
//                        name_TV.setText("停止");
//                        break;
//                }
//
//
//                item_community_post_msg_alert_point.setVisibility(View.VISIBLE);
//            } else {
//                item_community_post_msg_alert_point.setVisibility(View.GONE);
//            }
//            String content = topicNotice.getDisplayName();
//            contentTV.setText(content);
//            return arg1;
//        }
//
//        public void setSelectIndex(int i,PlayState playState) {
//            selectIndex = i;
//            pt = playState;
//            Log.i("HTML", "selectIndex:" + selectIndex);
//            notifyDataSetChanged();
//        }
//
//        public void setSelectIndex(PlayState playState) {
//            pt = playState;
//            notifyDataSetChanged();
//        }
//
//        @Override
//        protected void onReachBottom() {
//
//        }
//
//    }
//
//    //刷新选中状态
//    public void updataListViewSelectState(Song song) {
//        //播放结束
//        if(song ==null){
//            currentIndex =-1;
//            adapter.setSelectIndex(currentIndex,mPlaybackService.getPlayStatus());
//            return;
//        }
//        Song st;
//        for (int i = 0, j = adapter.getList().size(); i < j; i++) {
//            st = adapter.getList().get(i);
//            if (song.getDisplayName().equals(st.getDisplayName())) {
//                //自动播放
//                currentIndex = i;
//                adapter.setSelectIndex(i,mPlaybackService.getPlayStatus());
//                break;
//            }
//        }
//    }
//
//    //刷新选中状态
//    public void updataListViewSelectState(int index) {
//        adapter.setSelectIndex(index,mPlaybackService.getPlayStatus());
//    }
//
//    //刷新播放的状态
//    public void updataListViewItemPlayState(PlayState change) {
//        if(currentIndex ==-1)return;
//        adapter.setSelectIndex(change);
//    }
//
//
//    Subscriber subscriberUpdateSong = new Subscriber<Song>() {
//        @Override
//        public void onCompleted() {
//
//        }
//
//        @Override
//        public void onError(Throwable e) {
//
//        }
//
//
//        @Override
//        public void onNext(Song song) {
//            updataListViewSelectState(song);
//        }
//    };
//
//    Subscriber subscriberPlayState = new Subscriber<PlayState>() {
//        @Override
//        public void onCompleted() {
//
//        }
//
//        @Override
//        public void onError(Throwable e) {
//
//        }
//
//
//        @Override
//        public void onNext(PlayState change) {
//            updataListViewItemPlayState(change);
//        }
//    };
//
//}
